//
//  ShellGrabOrderCell.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/1.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellGrabOrderCell.h"
#import "ShellOrderModel.h"
#import "OYCountDownManager.h"

@interface ShellGrabOrderCell()
//icon
@property (nonatomic,strong) UIImageView *iconImageView;

//货币
@property (nonatomic,strong) UILabel *cureencyLab;

//地址
@property (nonatomic,strong) UILabel *addressLab;

//状态
@property (nonatomic,strong) UILabel *statusLab;

//时间
@property (nonatomic,strong) UIButton *timeBtn;

@property (nonatomic,strong) ShellOrderModel *orderModel;


@end

@implementation ShellGrabOrderCell

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:kCountDownNotification object:nil];
        
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType  = UITableViewCellAccessoryNone;
        
        self.backgroundColor = COLOR_BACKGROUND;
       
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    
    
    //状态图
    [self.contentView addSubview:self.iconImageView];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.left.mas_equalTo(35);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    
    //金额
    [self.contentView addSubview:self.cureencyLab];
    [self.cureencyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(23);
        make.left.mas_equalTo(self.iconImageView.mas_right).mas_offset(10);
        make.right.mas_equalTo(-100);
        make.height.mas_equalTo(17);
    }];

    
    //地址
    [self.contentView addSubview:self.addressLab];
    [self.addressLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.cureencyLab.mas_bottom).mas_offset(6);
        make.left.mas_equalTo(self.iconImageView.mas_right).mas_offset(10);
        make.right.mas_equalTo(-100);
        make.height.mas_equalTo(17);
    }];
    
    //状态
    [self.contentView addSubview:self.statusLab];
    [self.statusLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.right.mas_equalTo(-35);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(22);
    }];
    self.statusLab.bColor = UIColorHex(0xF4376D);
    self.statusLab.bWidth = 1;
    self.statusLab.cornerRadius = 11;
    
    //时间
    [self.contentView addSubview:self.timeBtn];
    [self.timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.statusLab.mas_bottom).mas_offset(6);
        make.right.mas_equalTo(-35);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(21);
    }];
    
    //虚线
    [self.contentView addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-1);
        make.right.mas_equalTo(-15);
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(1);
    }];
    
    
}


#pragma mark - actions

#pragma mark - custool

#pragma mark - 倒计时通知回调
- (void)countDownNotification {
    /// 计算倒计时
    NSInteger countDown = self.orderModel.remainingSeconds - kCountDownManager.timeInterval;
    if (countDown < 0) {
        countDown = 0;
    };
    
    /// 重新赋值
    NSString *time = [NSString stringWithFormat:@"%02zd:%02zd", (countDown/60)%60, countDown%60];
    [self.timeBtn setTitle:time forState:UIControlStateNormal];
    [self.timeBtn SG_imagePositionStyle:SGImagePositionStyleDefault spacing:6];

    /// 当倒计时到了进行回调
//    if (countDown < 1) {
//        [self.viewModel getOrder:YES];
        // self.statusLab.text = @"订单结束";
        // if (self.countDownZero) {
        //     self.countDownZero();
        // }
//    }
}
#pragma mark - get && set
- (void)setModelObject:(id)modelObject{
    self.orderModel = modelObject;
    
    if([self.orderModel.type isEqualToString:@"in"]){
        self.iconImageView.image = k_imageName(@"icon_zhuanru");//icon_chuanchu
    } else {
        self.iconImageView.image = k_imageName(@"icon_chuanchu");//
    }
    NSString *orderAmount = [NSString stringWithFormat:@"%f",self.orderModel.orderAmount].saveDecimal;
    self.cureencyLab.text = [NSString stringWithFormat:@"%@:%@",self.orderModel.orderCurrency,orderAmount];
    self.addressLab.text = [NSString stringWithFormat:@"%@:%@",LZLocalizedString(@"地址"),self.orderModel.targetAddress];
    self.statusLab.text = LZLocalizedString(@"待处理");
    [self countDownNotification];
}



- (UIImageView *)iconImageView{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
    }
    return _iconImageView;
}


//货币
- (UILabel *)cureencyLab{
    if(!_cureencyLab){
        _cureencyLab = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textFont(12)];
    }
    return _cureencyLab;
}

//地址
- (UILabel *)addressLab{
    if(!_addressLab){
        _addressLab = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textFont(12)];
    }
    return _addressLab;
}

//状态
- (UILabel *)statusLab{
    if(!_statusLab){
        _statusLab = [UILabel lz_createLabWithTextColor:UIColorHex(0xF4376D) font:k_textFont(12)];
        [_statusLab lz_centerAlignment];
    }
    return _statusLab;
}

//时间
- (UIButton *)timeBtn{
    if(!_timeBtn){
        _timeBtn = [UIButton lz_buttonTitle:@"" titleColor:UIColorHex(0x4AB2E7) fontSize:12];
        [_timeBtn setImage:k_imageName(@"icon_order_time") forState:UIControlStateNormal];
    }
    return _timeBtn;
}

- (UIImageView *)lineView{
    if (!_lineView) {
        _lineView = [[UIImageView alloc] initWithImage:k_imageName(@"icon_xuxian")];
    }
    return _lineView;
}
@end
